<!-- 计算下拉框中选中的个数 -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>Document</title>
</head>
<body>
    <form  name="selectForm" >
        <select name="musicTypes" multiple="multiple">
            <option selected="selected" >JAZZ</option>
            <option>ONE</option>
            <option>TWE</option>
            <option>REM</option>
            <option>ONE</option>
            <option>TWE</option>
            <option>REM</option>

        </select>
        <input id="btn" type="button" value="How many are selected?"
         />

    </form>
</body>
<script>
function howMany(selectObject) {
   var numberSelected = 0;
   for (var i = 0; i < selectObject.options.length; i++) {
    // ********【重点】
      if (selectObject.options[i].selected)
         numberSelected++;
   }
   return numberSelected;
}
window.onload=function()
{
    var obtn=document.getElementById('btn');
    obtn.onclick=function()
    {
        alert('Number of options selected: '+howMany(document.selectForm.musicTypes));
         // ********【重点】
    }
}

</script>
</html>